//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.ControlPanel.Controls;
using CommunityServer.ControlPanel.UI;
using CommunityServer.Controls;
using ResourceLinkButton = CommunityServer.ControlPanel.Controls.ResourceLinkButton;
using StatusMessage = CommunityServer.ControlPanel.Controls.StatusMessage;

namespace CommunityServer.ControlPanel.Settings
{
	/// <summary>
	/// Summary description for Posts.
	/// </summary>
	public class Posts : BaseSettingsPage
	{
		#region Child Controls

		protected YesNoRadioButtonList AnonymousUserPosting;
		protected YesNoRadioButtonList EnableCensorship;
		protected YesNoRadioButtonList EnableEmoticons;
		protected ForumRatingTypeRadioButtonList ForumRatingType;
		protected UserActivityDisplayRadioButtonList PostingActivityDisplay;
		protected YesNoRadioButtonList DisplayUserRankAsPicture;
		protected TextBox SearchPerPage;
		protected RequiredFieldValidator SearchPerPageValidator;
		protected TextBox TopPostersToDisplay;
		protected RequiredFieldValidator TopPostersToDisplayValidator;
		protected StatusMessage Status;
		protected ResourceLinkButton SaveButton;

		#endregion

		override protected void OnInit(EventArgs e)
		{

			this.SaveButton.Click += new EventHandler(SaveButton_Click);
			this.Load += new EventHandler(this.Page_Load);

			base.OnInit(e);
		}


		private void Page_Load(object sender, EventArgs e)
		{
			if ( !Page.IsPostBack ) 
			{
				this.Bind();
			}
		}


		protected void Bind()
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
			Status.Visible = false;

		    AnonymousUserPosting.SelectedValue = settings.EnableAnonymousUserPosting;
			EnableCensorship.SelectedValue = settings.EnableCensorship;
			EnableEmoticons.SelectedValue = settings.EnableEmoticons;
			ForumRatingType.SelectedValue = settings.SectionRatingType;
			PostingActivityDisplay.SelectedValue = settings.PostingActivityDisplay;
			DisplayUserRankAsPicture.SelectedValue = settings.DisplayUserRankAsPicture;
			SearchPerPage.Text = settings.SearchPostsPerPage.ToString();
			TopPostersToDisplay.Text = settings.MaxTopPostersToDisplay.ToString();

		}

		private void SaveButton_Click(object sender, EventArgs e) 
		{
			SiteSettings settings = CSContext.Current.SiteSettings;
			SaveSettings( settings );

			if(Page.IsValid)
			{
				Status.Success = true;
				Status.ResourceName = "Admin_SiteSettings_StatusSuccess";

				DataBind();
			}
			else
			{
				Status.Success = false;
				Status.ResourceName = "Admin_SiteSettings_StatusFailed";
			}

			Status.Visible = true;
		}

		protected virtual void SaveSettings( SiteSettings settings ) 
		{

			settings.EnableAnonymousUserPosting = AnonymousUserPosting.SelectedValue;
			settings.EnableCensorship = EnableCensorship.SelectedValue;
			settings.EnableEmoticons = EnableEmoticons.SelectedValue;
			settings.SectionRatingType = ForumRatingType.SelectedValue;
			settings.PostingActivityDisplay = PostingActivityDisplay.SelectedValue;
			settings.DisplayUserRankAsPicture = DisplayUserRankAsPicture.SelectedValue;

			try 
			{ 
				settings.SearchPostsPerPage = Int32.Parse( SearchPerPage.Text ); 
				if(settings.SearchPostsPerPage < 1)
					SearchPerPageValidator.IsValid = false;
			}
			catch { SearchPerPageValidator.IsValid = false; }

			try
			{
				settings.MaxTopPostersToDisplay = Int32.Parse(TopPostersToDisplay.Text);
				if (Int32.Parse(TopPostersToDisplay.Text) < 1)
					TopPostersToDisplayValidator.IsValid = false;
			}
			catch { TopPostersToDisplayValidator.IsValid = false; }


			if(Page.IsValid)
				SiteSettingsManager.Save(settings);

		}

	}
}
